#include "chatfriend.h"
#include "ui_chatfriend.h"
#include "sendfile.h"
ChatFriend::ChatFriend(QTcpSocket *s, QString user, QString chatW, QByteArray userimg, QPixmap usericon, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatFriend)
{
    ui->setupUi(this);
    _socket = s;
    _username = user;
    _chatWith = chatW;
    _userimg = userimg;
    _usericon = usericon;
    ui->label_chatName->setText(_chatWith);
}

ChatFriend::~ChatFriend()
{
    delete ui;
}

// 通过调用messageW的setText方法
void ChatFriend::dealMessage(QNChatMessage *messageW, QListWidgetItem *item, QString text, QString time,  QNChatMessage::User_Type type)
{
    messageW->setFixedWidth(this->width());
    QSize size = messageW->fontRect(text);
    item->setSizeHint(size);
    messageW->setText(text, time, size, type);
    ui->listWidget->setItemWidget(item, messageW);
    ui->listWidget->scrollToBottom();
}

void ChatFriend::dealMessageTime(QString curMsgTime)
{
    bool isShowTime = false;
    if(ui->listWidget->count() > 0) {
        QListWidgetItem* lastItem = ui->listWidget->item(ui->listWidget->count() - 1);
        QNChatMessage* messageW = (QNChatMessage*)ui->listWidget->itemWidget(lastItem);
        int lastTime = messageW->time().toInt();
        int curTime = curMsgTime.toInt();
        qDebug() << "curTime lastTime:" << curTime - lastTime;
        isShowTime = ((curTime - lastTime) > 60); // 两个消息相差一分钟
//        isShowTime = true;
    } else {
        isShowTime = true;
    }
    if(isShowTime) {
        QNChatMessage* messageTime = new QNChatMessage(_userimg, ui->listWidget->parentWidget());
        QListWidgetItem* itemTime = new QListWidgetItem(ui->listWidget);

        QSize size = QSize(this->width(), 40);
        messageTime->resize(size);
        itemTime->setSizeHint(size);
        messageTime->setText(curMsgTime, curMsgTime, size, QNChatMessage::User_Time);
        ui->listWidget->setItemWidget(itemTime, messageTime);
    }
}

void ChatFriend::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);

//    ui->textEdit->resize(this->width() - 20, ui->widget->height() - 20);
//    ui->textEdit->move(10, 10);

    for(int i = 0; i < ui->listWidget->count(); i++) {
        QNChatMessage* messageW = (QNChatMessage*)ui->listWidget->itemWidget(ui->listWidget->item(i));
        QListWidgetItem* item = ui->listWidget->item(i);

        dealMessage(messageW, item, messageW->text(), messageW->time(), messageW->userType());
    }
}

void ChatFriend::getMsg(QString whoSend, QByteArray whoSendImg, QString msg) // 接收消息
{
    // 使用qnchatmessage，创建新消息，总共四条
    QString time = QString::number(QDateTime::currentDateTime().toTime_t()); //时间戳
    dealMessageTime(time);
    QNChatMessage* messageW = new QNChatMessage(whoSendImg, ui->listWidget->parentWidget());
    QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    dealMessage(messageW, item, msg, time, QNChatMessage::User_She);
}

void ChatFriend::on_btn_send_clicked()
{
    // 这里只能发送消息，并不能接收消息了。接收全部都在Home那里

    QString msg = ui->textEdit->toPlainText();
    ui->textEdit->clear();

    // 使用qnchatmessage，创建新消息，总共四行代码
    QString time = QString::number(QDateTime::currentDateTime().toTime_t()); //时间戳
    dealMessageTime(time);
    QNChatMessage* messageW = new QNChatMessage(_userimg, ui->listWidget->parentWidget());
    QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    dealMessage(messageW, item, msg, time, QNChatMessage::User_Me); // 我

    // 发送给服务端
    QString action = "SEND_MSG_FRIEND";
    QDataStream stream(_socket);
    stream << action;
    QString whoGet = _chatWith;
    stream << whoGet << msg;
    stream << _username << _userimg;

//    // 无法接收消息了，全由Home去接收
//    QString response;
//    stream >> response;
//    qDebug() << "row 105 response is " << response;
//    if (response == "TRUE") {
//        qDebug() << "发送成功";
//    } else {
//        QMessageBox::critical(this, "发送失败", "对方不在线");
//    }
}

void ChatFriend::on_btn_file_clicked()
{
    SendFile *sf = new SendFile(_socket, _username, _chatWith);
    sf->show();
}
